<?php
function category_model_get_child_ids($category_id = 0) {
	$category_id = intval($category_id);
	$category_ids_array = category_model_get_child_ids_array($category_id);
	$category_ids = implode(',', $category_ids_array);
	return $category_ids;

}

function category_model_get_child_ids_array($category_id = 0) {
	$category_id = intval($category_id);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select id from categorys where parent_id=" . $category_id;
	$categorys_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$categorys_array[] = $row['id'];
				$category_child_ids = category_model_get_child_ids_array($row['id']);
				$categorys_array = array_merge($categorys_array, $category_child_ids);
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $categorys_array;
}

function category_model_insert($category_name = '', $category_alias = '', $parent_id = 0, $status = 1, $keywords = '', $description = '') {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$category_name = trim($category_name);
	$category_alias = trim($category_alias);
	$create_time = date('Y-m-d H:i:s', time());
	$hash = md5($create_time);
	$status = intval($status);
	$parent_id = intval($parent_id);
	$sql = "insert into categorys(hash,parent_id,category_alias,category_name,status)values(?,?,?,?,?)";

	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {

			return false;
		}

		if (!$stmt->bind_param('sissi', $hash, $parent_id, $category_alias, $category_name, $status)) {
			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {
			$stmt->close();

			return false;
		} else {
			$id = $db->insert_id;
			$stmt->close();

			return $id;
		}

	} catch (Exception $e) {

		// TODO
		echo $e;
		return false;
	}
}

function category_model_get_by_id($category_id = 0) {
	if ($category_id == 0) {
		return false;
	}

	$category_id = intval($category_id);

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from categorys where `id`='" . $category_id . "' limit 1";
	$category_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			$category_data = mysqli_fetch_assoc($result);
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $category_data;
}

/**
 * 根据hash获取数据
 *
 * @param  string
 * @return [type]
 */
function category_model_get_by_hash($hash = '') {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from categorys where `hash`='" . $hash . "' limit 1";
	$category_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/category/helper.php';
			require_once APP_PATH . '/post/helper.php';
			$category_data = mysqli_fetch_assoc($result);
			if (!empty($category_data)) {
				$category_data['url'] = category_helper_get_url($category_data);
				//获取分类信息
				$category = category_helper_get_category($category_data['parent_id']);
				$category_data['parent_category_name'] = $category['category_name'];
			} else {
				$category_data = false;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $category_data;
}

/**
 * 根据hash获取数据
 *
 * @param  string
 * @return [type]
 */
function category_model_get_by_category_path($category_path = '') {
	if (empty($category_path)) {
		return false;
	}
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from categorys where `category_path`='" . $category_path . "' limit 1";
	$category_data = false;
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			require_once APP_PATH . '/category/helper.php';
			require_once APP_PATH . '/post/helper.php';
			$category_data = mysqli_fetch_assoc($result);
			if (!empty($category_data)) {
				$category_data['url'] = category_helper_get_url($category_data);
				//获取分类信息
				$category = category_helper_get_category($category_data['parent_id']);
				$category_data['parent_category_name'] = $category['category_name'];
			} else {
				$category_data = false;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $category_data;
}

/**
 * 触发系统生成纯静态化页面业务逻辑
 * 有3种方案：
 * 第一：定时扫描程序(利用crontab来处理)
 * 第二：手动触发方式，人为触发
 * 第三：页面添加缓存时间，在页面中控制时间来操作
 */
function category_model_get_list($page = 1, $page_size = 8) {
	$page = intval($page);
	$limit = ($page - 1) * $page_size;

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "select * from categorys where `status` = 1 limit " . $limit . "," . $page_size;
	$categorys_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$row['url'] = category_helper_get_url($row);
				$row['parent_category_name'] = category_helper_get_category($row['parent_id']);
				$row['post_count'] = category_helper_get_post_count_by_category_id($row['id']);
				$categorys_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $categorys_array;
}

function category_model_get_all_list() {
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	require_once APP_PATH . 'category/helper.php';
	require_once APP_PATH . 'post/helper.php';
	$sql = "select * from categorys";
	$categorys_array = array();
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);

		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$parent_category_name = category_helper_get_category($row['parent_id']);
				$row['parent_category_name'] = $parent_category_name['category_name'];
				$row['status'] = ($row['status'] == 1) ? '已发布' : '隐藏';
				$row['is_build'] = ($row['is_build'] == 1) ? '已生成HTML' : '未生成';
				$categorys_array[] = $row;
			}
		} else {
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// TODO
	}
	return $categorys_array;
}

function category_model_get_all_list_count($category_id = 0, $keyword = '') {
	$where = " 1=1 ";

	//数据安全过滤
	$keyword = trim($keyword);
	//分类
	$category_id = intval($category_id);
	require_once APP_PATH . DIRECTORY_SEPARATOR . 'category/model.php';
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	if (!empty($category_id)) {
		//获取下级所有分类ID
		$category_child_ids = category_model_get_child_ids($category_id);
		$category_ids = "";
		//分类ID通过,链接
		if (empty($category_child_ids)) {
			$category_ids = $category_id;
		} else {
			$category_ids = $category_child_ids . ',' . $category_id;
		}

		$where .= "and category_id in (" . $category_ids . ") ";
	}

	if (!empty($keyword)) {
		$where .= "and (category_name like '%" . $keyword . "%' or category_alias like '%" . $keyword . "%')";
	}

	//sql查询
	$sql = "select count(id) as count from categorys where " . $where;
	$result_count = 0;
	try
	{
		//创建实力
		$db = Db::getInstance()->connect();
		//查询
		$result = mysqli_query($db, $sql);
		//结果处理
		if ($result) {
			$row = mysqli_fetch_assoc($result);
			if ($row) {
				$result_count = $row['count'];
			}

		} else {
			//SQL报错停止
			die('mysql error' . mysqli_error($db));
		}

	} catch (Exception $e) {
		// 其他异常显示
	}
	//返回
	return $result_count;
}

function category_model_delete_by_hash($hash = '') {
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}
	$category_data = category_model_get_by_hash($hash);

	//删除数据库记录
	$sql = "delete from categorys where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {

			return false;
		}

		if (!$stmt->bind_param('s', $hash)) {

			$stmt->close();

			return false;
		}

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			require_once APP_PATH . '/category/build.php';
			//删除物理文件
			category_delete_html_by_hash($category_data);
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
		return false;
	}
}

function category_model_update_by_hash($hash = '', $category_name, $parent_id = 0, $category_alias = '', $status = 0, $keywords = '', $description = '') {

	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$hash = filter_data($hash);
	if (empty($hash)) {
		return false;
	}

	$category_name = trim($category_name);
	$modify_time = date('Y-m-d H:i:s', time());
	$status = intval($status);
	$sql = "update categorys set parent_id=?,category_name=?,category_alias=?,category_path=?,modify_time=?,status=?,keywords=?,description=? where hash=?";
	try
	{
		$db = Db::getInstance()->connect();
		$stmt = $db->prepare($sql);
		if (!$stmt) {
			echo $db->error;
			return false;
		}

		if (!$stmt->bind_param('issssisss', $parent_id, $category_name, $category_alias, $category_path, $modify_time, $status, $keywords, $description, $hash)) {

			$stmt->close();

			return false;
		}

		$category_path = strtolower('/category/' . $category_alias);

		if (!$stmt->execute()) {

			$stmt->close();

			return false;
		} else {
			$stmt->close();

			require_once APP_PATH . '/category/build.php';
			//更新HTML
			category_build_page_by_hash($hash);
			return true;
		}
	} catch (Exception $e) {
		// TODO
		echo $e;
	}
}

/**
 * 根据hash更新统计信息
 *
 * @param  string
 * @return [type]
 */
function category_model_update_is_build_by_hash($hash = '', $is_build = 1) {
	if (empty($hash)) {
		return false;
	}
	$hash = filter_data($hash);
	$is_build = intval($is_build);
	$build_time = date('Y-m-d H:i:s', time());
	// 引入数据库链接操作
	require_once CORE_PATH . 'db.php';
	$sql = "update categorys set is_build='" . $is_build . "' ,build_time='" . $build_time . "'  where `hash`='" . $hash . "'";
	try
	{
		$db = Db::getInstance()->connect();
		$result = mysqli_query($db, $sql);
		if ($result) {
			return true;
		} else {
			return false;
		}
	} catch (Exception $e) {
		// TODO
		return false;
	}
	return false;
}
